import http from "./http";
import {CLIENT} from "@/common/const";

/**
 * 密码登录
 * @param <String> phoneNumber:手机号
 * @param <String> password:密码
 * @return <Promise> Array
 */
export async function passwordLogin(option) {
  const url = `${CLIENT}/client/passwordLogin.htm`;
  //指定默认参数
  const def = {
    phoneNumber: "",
    password: "",
  };
  const params = Object.assign(def, option);
  const res = await http.post(url, params);
  return res;
}

/**
 * 验证码登录
 * @param <String> phoneNumber: 手机号
 * @param <String> phoneAuthCode: 手机验证码
 * @return <Promise> Object
 */
export async function phoneAuthcodeLogin(option) {
  const url = `${CLIENT}/client/phoneAuthcodeLogin.htm`;
  //指定默认参数
  const def = {
    phoneNumber: "",
    phoneAuthCode: "",
  };
  const params = Object.assign(def, option);
  const res = await http.post(url, params);
  if (res.status == 1) {
    // const clientCentralToken = (window.clientCentralToken =
    //   res.data.centralToken);
    // localStorage.setItem("clientCentralToken", clientCentralToken);
  }
  return res;
}

/**
 * 发送验证码
 * @param <String> phoneNumber: 手机号
 * @param <String> imageAuthcode: 图片验证码
 * @return <Promise> Object
 */

export async function sendPhoneAuthcode(option) {
  const url = `${CLIENT}/client/sendPhoneAuthcode.htm`;
   //指定默认参数
   const def = {

    ...option
  };
  const res =await http.post(url,def);
  return res;
}

/**
 * 注册
 * @param <String> phoneNumber: 手机号
 * @param <String> phoneAuthCode: 验证码
 * @param <String> password: 密码
 * @param <String> versionCode: 版本号
 * @param <String> checkCode: 产品校验码
 * @param <String> projectSource: 3天龙博弈客户端 7约牛股票客户端
 * @return <Promise> Object
 */
export async function register(option) {
  const url = `${CLIENT}/client/register.htm`;
  //指定默认参数
  const def = {
    phoneNumber: "",
    phoneAuthCode: "",
    password: "",
    versionCode: "", //版本号
    checkCode: "", //产品校验码
    projectSource: "3",
    ...option,

  };
  const res = await http.post(url, def);
  return res;
}
/**
 * 用户行为统计
 * @param {Object} option
 * @return {Promise} Array
 */
export async function userClickUpdate(option) {
  const url = `${CLIENT}/client/insertUserBeInfoDate.htm`;
  const data = JSON.stringify(option);
  const params = {
    data: data,
    ...option
  };
  const res = await http.post(url, params);
  return res;
}
/**
 * 退出登录
 * @param <String> clientCentralToken: token
 * @return <Promise> Object
 */
export async function logout() {
  const url = `${CLIENT}/client/logout.htm`;
  //指定默认参数
  //   const def = {
  //     clientCentralToken: window._clientCentralToken
  //   };
  //   const params = Object.assign(def, option);
  const res = await http.post(url);
  return res.data;
}

/**
 * 获取用户信息
 * @param <String> clientCentralToken: token
 * @return <Promise> Object
 */
export async function userInfo() {
  const url = `${CLIENT}/client/userInfo.htm`;
  //   指定默认参数
  const params = {
    clientCentralToken: window.clientCentralToken,
  };
  
  const res = await http.get(url, { params });
  return res.data;
}

/**
 * 重置密码
 * @param <String> phoneNumber: 手机号
 * @param <String> phoneAuthCode 手机验证码
 * @param <String> password: 手机密码
 * @return <Promise> Object
 */
export const resetPassword = async (option) => {
  const url = `${CLIENT}/client/resetPassword.htm`;
  const def = {
    phoneNumber: "",
    phoneAuthCode: "",
    password: "",
    ...option
  };
  
  const res = await http.post(url, def);
  return res;
};

/**
 * 获取领取状态
 * @param <String> clientCentralToken: token
 * @return <Promise> Object
 */
export async function findUserTarget() {
  const url = `${CLIENT}/client/findUserTarget.htm`;
  //   指定默认参数
  const params = {
    clientCentralToken: window.userInfo.centralToken,
  };
  
  const res = await http.get(url, params );
  return res.data;
}
/**
 * 获取系统时间
 * @return <Promise> Object
 */

export  function systemTime() {
  const url = `${CLIENT}/client/systemTime.htm`;
  const res = http.get(url);
  return res.data;
}



/**
 * 重置密码
 * @param <String> phoneNumber: 手机号
 * @param <String> password  加密后原密码
 * @param <String> newPassword: 加密后新密码
 * @param <String> loginVersion: 登陆版本
 * @param <String> clientVersion: 客户端版本
 * @param <String> clientDeviceId: 设备id
 * @return <Promise> Object
 */

export const updatePassword = async (option) => {
  const url = `${CLIENT}/client/updatePassword.htm`;
  const def = {
    phoneNumber:'',
    password:'',
    newPassword:'',
    loginVersion:'',
    clientVersion:'',
    clientDeviceId:'',
    ...option
  };
  const res = await http.post(url, def);
  return res;
};